<template>
  <div class="layout-container">
    <div class="back-header">
      <el-page-header title="返回" :content="pageHeader" @back="goBack" />
    </div>
    <el-form
      ref="refruleForm"
      :model="form"
      :rules="rules"
      label-width="130px"
      style=""
    >
         <el-form-item label="内容标题"  prop="title" style="width:500px">
           <el-input v-model="query.title" disabled placeholder="请输入内容标题"></el-input>
        </el-form-item>
        <el-form-item label="内容：" prop="content"  style="width:90%">
        <!-- <editor v-model="myValue" :init="init" :disabled="disabled" :id="tinymceId"></editor> -->
            <!-- <el-input
            type="textarea"
            v-model="form.content"
            placeholder="请输入内容"
            rows="5"
            maxlength="200"
            show-word-limit
            /> -->
            <WangEditor ref="WangEditors" :modelValue="form.content"></WangEditor>
        </el-form-item>
        
      <el-form-item >
        <div class="flex-end">
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSubmit"
            >提交</el-button
            >
        </div>
        
      </el-form-item>
    </el-form>
  </div>
</template>

<script  lang="ts">

</script>
<script setup lang="ts">
import {ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import { sendMessage } from "@/api/service/message.ts";
import { updateByDictCode,notifyDetail,getByDictType} from "@/api/notice.ts";
// import WangEditor from "@/components/WangEditor/WangEditor.vue";
import WangEditor from "@/components/tinymce1/index.vue";
const router = useRouter();
const route = useRoute();
const pageHeader = ref("新建文章");
const query = ref({...route.query});
const WangEditors = ref()

if(query.value.id){
    pageHeader.value = '修改文章'
    getByDictType({dict_code:query.value.id}).then(res => {
        if(res.code == 200){
            form.content = res.data.dict_value
        }
    })    
}



const refruleForm: any = ref(null);
const form: any = reactive({
  title:'',
  content: "", // string 分类大图
  column_id: "",
});

// 必填校验
const rules = reactive({
  content: [
    {
      message: "请输入通知内容",
      trigger: "blur",
    },
  ],
  column_id: [
    {
      required: true,
      message: "请选择栏目",
      trigger: "blur",
    },
  ],
});

// 返回
const goBack = () => {
  router.push({
    path: "/backstagerpzzm/notificationMessage/sysArticleList",
  });
};
// 取消
const handleCancel = () => {
  router.push({
    path: "/backstagerpzzm/notificationMessage/sysArticleList",
  });
};
// 确认发送
const handleSubmit = () => {
   form.content = WangEditors.value.getContent()
  if(!form.content){
    return 
  }
  refruleForm.value.validate((valid: Boolean) => {
    if (valid) {
        if(query.value.id){
            sendMessageEdit()
        }else{
            sendMessageFun()
        }
    } else {
      console.log("error submit!!");
      return false;
    }
  });
};
// 消息发送事件
const sendMessageFun = () => {
  let data = {
    ...form
  }
  addNotify(data).then((res: any) => {
    if (res.code === 200) {
      ElMessage.success("新增成功");
      goBack()
    }
  })
}// 消息发送事件
const sendMessageEdit = () => {
  let data = {
    dict_code:query.value.id,
    dict_value:form.content
  }
  updateByDictCode(data).then((res: any) => {
    if (res.code === 200) {
      ElMessage.success("修改成功");
      goBack()
    }
  })
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: auto;
  .back-header {
    padding: 20px;
    // width: 100%;
    border-bottom: 1px solid #fafafa;
  }
}
.flex-end{
    width: 90%;
    display: flex;
    justify-content: flex-end;
}
.flex-start-nowrap{
    display: flex;
    justify-content: flex-start;
    flex-wrap: nowrap;
}
</style>
